package cn.wolfcode.rbac.web.controller;

import cn.wolfcode.rbac.query.CustomerReportQuery;
import cn.wolfcode.rbac.service.ICustomerReportService;
import cn.wolfcode.rbac.util.MessageUtil;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/customerReport")
public class CustomerReportController {
    @Autowired
    private ICustomerReportService customerReportService;
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo")CustomerReportQuery qo){
        model.addAttribute("result",this.customerReportService.selectCustomerReport(qo));
        return "customerReport/list";
    }
    @RequestMapping("listByBar")
    public String listByBar(Model model,@ModelAttribute("qo")CustomerReportQuery qo){
        PageInfo pageInfo=this.customerReportService.selectCustomerReport(qo);
        List<Map> list=pageInfo.getList();
        ArrayList xList=new ArrayList();
        ArrayList yList=new ArrayList();
        for (Map map:list){
            xList.add(map.get("groupType"));
            yList.add(map.get("number"));
        }
        model.addAttribute("xList", JSON.toJSONString(xList));
        model.addAttribute("yList", JSON.toJSONString(yList));
        model.addAttribute("groupTypeName", MessageUtil.changMsg(qo));
        return "customerReport/listByBar";
    }
    @RequestMapping("listByPie")
    public  String listByPie(Model model,@ModelAttribute("qo")CustomerReportQuery qo){
        PageInfo pageInfo=this.customerReportService.selectCustomerReport(qo);
        List<Map> list=pageInfo.getList();
        ArrayList xList=new ArrayList();
        ArrayList yList=new ArrayList();
        BigDecimal maxAmount = BigDecimal.ZERO;
        for (Map map:list){
            Map<String, Object> data = new HashMap();
            data.put("name", map.get("groupType"));
            data.put("value", map.get("number"));
            xList.add(map.get("groupType"));
            yList.add(data);
            // yList.add(map.get("number"));
        }
        model.addAttribute("xList", JSON.toJSONString(xList));
        model.addAttribute("yList", JSON.toJSONString(yList));
        model.addAttribute("groupTypeName", MessageUtil.changMsg(qo));
        model.addAttribute("customerNums", maxAmount);
        return "customerReport/listByPie";
    }

}
